<?php

namespace Admin\Controller;

use Common\Controller\BaseController;

/**
 * 后台首页
 */
class IndexController extends BaseController {

    /**
     * 后台首页
     */
    public function index() {
        $myAuth = new \Common\Lib\MyAuth();
        $ruleList = $myAuth->getUserRules(session('userInfo.id'));

        if (!empty($ruleList)) {
            //获取权限中类型为链接
            $tmp = array();
            foreach ($ruleList as $item) {
                if ($item['type'] == 0) {
                    $item['url'] = U($item['name']);
                    $tmp[] = $item;
                }
            }
            $ruleList = $tmp;
        }

        $ruleList = genTree($ruleList);

        $this->assign('ruleList', $ruleList);
        $this->display();
    }

    /**
     * 欢迎页面
     */
    public function welcome() {
        $this->display();
    }

    /**
     * 登陆
     */
    public function login() {
        if (IS_POST) {
            $phone = I('post.phone', '');
            $password = I('post.password', '');
            $verify = I('post.verify', '');

            if (empty($phone)) {
                $this->error('手机号不能为空');
            }
            if (empty($password)) {
                $this->error('密码不能为空');
            }
            if (empty($verify)) {
                $this->error('验证码不能为空');
            }
            if (!checkVerify($verify, 'login')) {
                $this->error('验证码错误');
            }
            $users = M('Users');

            //专业人员并且用户状态正常
            $existUser = $users->alias('u')
                    ->join('__USER_GROUP_ACCESS__ uga ON u.id = uga.user_id')
                    ->join('__USER_GROUP__ ug ON uga.group_id = ug.id')
                    ->where("u.status=1 AND u.phone='{$phone}'")
                    ->field('u.*,ug.name as rolename')
                    ->find();
                    
            if (empty($existUser)) {
                $this->error('该用户不存在');
            }
            $password = md5($password);
            if ($existUser['password'] != $password) {
                $this->error('密码错误');
            }
            if (!$existUser['status'] || $existUser['rolename'] == '非专业人员') {
                $this->error('禁止登陆');
            }
            $users->where("id={$existUser['id']}")->save(array('last_login_time' => time(), 'last_login_ip' => get_client_ip()));
            session('userInfo', $existUser);
            $this->redirect(U('Admin/Index/index'));
        } else {
            $this->display();
        }
    }

    /**
     * 退出
     */
    public function logout() {
        session(null);
        $this->redirect(U('Admin/Index/login'));
    }

    /**
     * 验证码
     */
    public function verify() {
        showVerify('login');
    }

}
